﻿using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using Microsoft.Practices.ObjectBuilder2;
using TciNewsReader_2014_04.Model;
using TciNewsReader_2014_04.VmHelpers;
namespace TciNewsReader_2014_04
{
    public partial class ViewModel
    {
        public ObservableCollection<AgeInfo> AgeSelectors { get; set; }
        private AgeInfo _ageSelectedItem;
        public AgeInfo AgeSelectedItem
        {// bindind to the menu
            get { return _ageSelectedItem; }
            set
            {
                if (value != _ageSelectedItem)
                {
                    _ageSelectedItem = value;
                    OnPropertyChanged("AgeSelectedItem");
                    UpdateAgeStatus();
                    ItemWrappers.ForEach(FilteringHandler);
                    FeedInfos.ForEach(q=>q.RaiseNotifyOnCounts());
                    _itemWrappersListCollectionView.Refresh();
                    UpdateCountsStatus();
                }
            }
        }
        private string _ageStatus;
        public string AgeStatus
        {
            [DebuggerStepThrough] get { return _ageStatus; }
            [DebuggerStepThrough]
            set
            {
                if (value != _ageStatus)
                {
                    _ageStatus = value;
                    OnPropertyChanged("AgeStatus");
                }
            }
        }
        private bool ItemFilterDelegate(object obj)
        {// binded to the lcv
            var itemWrapper = obj as ItemWrapper;
            if (itemWrapper != null)
            {
                FilteringHandler(itemWrapper);
                return itemWrapper.FilterStatus == FilterStatus.FilteredIn;
            }
            return false;
        }
        private void FilteringHandler(ItemWrapper iw)
        {// internal filtering
            if (iw == null) return;
            double hours = -AgeSelectedItem.HourOffsetFromNow;
            if (iw.ItemPublishDate.AddHours(hours) < DateTime.Now)
            {
                iw.FilterStatus = FilterStatus.FilteredOut;
                return;
            }
            if (!iw.OwnerInfo.IncludeInView)
            {
                iw.FilterStatus = FilterStatus.FilteredOut;
                return;
            }
            if (iw.ItemStatus == ItemStatus.Current)
            {
                iw.FilterStatus = FilterStatus.FilteredIn;
                return;
            }
            if (HideReadItems)
            {
                if (iw.ItemStatus == ItemStatus.Read)
                {
                    iw.FilterStatus = FilterStatus.FilteredOut;
                    return;
                }
            }
            iw.FilterStatus = FilterStatus.FilteredIn;
        }
        private void UpdateAgeStatus()
        {
            AgeStatus = String.Format("{0}", AgeSelectedItem.Description);
        }
        private void InitialiseAgeFilter()
        {
            AgeSelectors = new ObservableCollection<AgeInfo>
            {
                new AgeInfo {Description = "Last hour", HourOffsetFromNow = -1},
                new AgeInfo {Description = "Last 3 hours", HourOffsetFromNow = -3},
                new AgeInfo {Description = "Last 6 hours", HourOffsetFromNow = -6},
                new AgeInfo {Description = "Last 12 hours", HourOffsetFromNow = -12},
                new AgeInfo {Description = "Last 24 hours", HourOffsetFromNow = -24},
                new AgeInfo {Description = "Last week", HourOffsetFromNow = -24*7},
                new AgeInfo {Description = "Last month", HourOffsetFromNow = -24*30},
                new AgeInfo {Description = "Last year", HourOffsetFromNow = -24*365},
                new AgeInfo {Description = "All", HourOffsetFromNow = -10*24*365}
            };
            AgeSelectedItem = AgeSelectors[AgeSelectors.Count - 1];
            AgeSelectedItem.IsChecked = true;
            foreach (AgeInfo si in AgeSelectors)
            {
                si.PropertyChanged += (s, a) =>
                {
                    if (a.PropertyName != "IsChecked") return;
                    AgeInfo ageInfo = s as AgeInfo;
                    if (ageInfo != null)
                    {
                        if (!ageInfo.IsChecked) return;
                        foreach (AgeInfo ainfo in AgeSelectors)
                        {
                            if (ainfo.Description != ageInfo.Description)
                            {
                                ainfo.IsChecked = false;
                            }
                        }
                        AgeSelectedItem = ageInfo;
                    }
                };
            }
        }
    }
}
